<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <h5>注册</h5>
        <div>
            <input placeholder="用户名" v-model.lazy="username" type="text" />
            <img v-show="isLoading" src="../assets/img/loading.gif" alt="" srcset="">
            <span v-show="!isLoading" :style=" `color:${result.state?'green':'red'}`">{{ result.msg }}</span>
        </div>
        <div>
            <input placeholder="密码" type="text" />
        </div>
        <div>
            <input type="button" value="注册">
        </div>
    </div>
    <script type="module">
        import { createApp } from "../assets/vue3/vue.esm-browser.js";
        import axios from "../assets/axios/axios.js"
        createApp({
            data() {
                return {
                    username:"",
                    isLoading:false,
                    result:{}
                }
            },
            watch: {
                async username(newValue, oldValue) {
                    try {
                        this.isLoading = true;
                        let { data } = await axios.get("http://127.0.0.1:3000/api/checkUsername",{
                            params:{
                                username:newValue
                            }
                        })
                        this.result = data;
                    } catch (error) {
                        console.log(error)
                    } finally {
                        this.isLoading = false;
                    }
                }
            },
        }).mount("#app")
    </script>
</body>
</html>